package com.iplant.aps.serviceImpl.utils.mrp;

import com.iplant.aps.service.po.aps.APSTaskPartPointDay;
import com.iplant.aps.service.po.oms.OMSOrder;
import com.iplant.pdm.service.po.mss.MSSBOMItem;
import com.iplant.wms.service.basicenum.mrp.MRPDeliveryStatus;
import com.iplant.wms.service.po.mrp.MRPDelivery;
import com.iplant.wms.service.po.mrp.MRPDeliveryItem;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
public class MRPDeliveryUtils {

    private static MRPDeliveryUtils Instance = null;

    public static MRPDeliveryUtils getInstance() {
        if (Instance == null) {
            Instance = new MRPDeliveryUtils();
        }
        return Instance;
    }

    private MRPDeliveryUtils() {

    }

    /**
     * 查出所有存在配送需求，
     * 对比主键如果存在就修改,只能修改计划数，不能修改已配料数 ，
     * 如果不存在new
     * 如果存在已配的配送需求，且当日需求数量降低时，将后续日期的需求减少对应数量
     * 一般物料已配送，不会变更计划与物料配送需求
     *
     * @param wOMSOrder
     * @param wAPSTaskPartPointDay
     * @param wMSSBOMItem
     */
    public MRPDelivery MRP_CreateDelivery(OMSOrder wOMSOrder,
                                          APSTaskPartPointDay wAPSTaskPartPointDay, MSSBOMItem wMSSBOMItem) {


        MRPDelivery wResult = new MRPDelivery();
        wResult.FactoryID = wOMSOrder.FactoryID;
        wResult.FactoryName = wOMSOrder.FactoryName;
        wResult.FactoryCode = wOMSOrder.FactoryCode;
        wResult.ProductID = wOMSOrder.ProductID;
        wResult.ProductCode = wOMSOrder.ProductCode;
        wResult.ProductName = wOMSOrder.ProductName;
        wResult.LineID = wOMSOrder.LineID;
        wResult.LineName = wOMSOrder.LineName;
        wResult.LineCode = wOMSOrder.LineCode;
        wResult.CustomerID = wOMSOrder.CustomerID;
        wResult.CustomerName = wOMSOrder.CustomerName;
        wResult.CustomerCode = wOMSOrder.CustomerCode;
        wResult.PartNo = wOMSOrder.PartNo;
        wResult.OrderID = wOMSOrder.ID;
        wResult.OrderNo = wOMSOrder.Code;
        wResult.WBSNo = wOMSOrder.WBSNo;
        wResult.MaterialID = wMSSBOMItem.MaterialID;
        wResult.MaterialName = wMSSBOMItem.MaterialName;
        wResult.MaterialCode = wMSSBOMItem.MaterialNo;
        wResult.MaterialTypeID = wMSSBOMItem.MaterialTypeID;
        wResult.MaterialTypeName = wMSSBOMItem.MaterialTypeName;
        wResult.MaterialTypeCode = wMSSBOMItem.MaterialTypeCode;
        wResult.MaterialGroupID = wMSSBOMItem.MaterialGroupID;
        wResult.MaterialGroupCode = wMSSBOMItem.MaterialGroupCode;
        wResult.MaterialGroupName = wMSSBOMItem.MaterialGroupName;
        wResult.MaterialFullGroup = wMSSBOMItem.MaterialFullGroup;
        wResult.UnitID = wMSSBOMItem.UnitID;
        wResult.UnitText = wMSSBOMItem.UnitText;
        wResult.UnitCode = wMSSBOMItem.UnitCode;
        wResult.PlanFQTY = wMSSBOMItem.MaterialNumber * wAPSTaskPartPointDay.DPlantNum;
        wResult.DemandDate = wAPSTaskPartPointDay.StationDate;
        wResult.ReplaceType = wMSSBOMItem.ReplaceType;
        wResult.OutSourceType = wMSSBOMItem.OutsourceType;
        wResult.OriginalType = wMSSBOMItem.OriginalType;
        wResult.DisassyType = wMSSBOMItem.DisassyType;
        wResult.Status = MRPDeliveryStatus.Todo.getValue();

        return wResult;
    }


    public MRPDelivery MRP_CreateDelivery(OMSOrder wOMSOrder,
                                          List<MRPDeliveryItem> wMaterialItemList) {

        MRPDelivery wResult = new MRPDelivery();

        wResult.FactoryID = wOMSOrder.FactoryID;
        wResult.FactoryName = wOMSOrder.FactoryName;
        wResult.FactoryCode = wOMSOrder.FactoryCode;
        wResult.ProductID = wOMSOrder.ProductID;
        wResult.ProductCode = wOMSOrder.ProductCode;
        wResult.ProductName = wOMSOrder.ProductName;
        wResult.LineID = wOMSOrder.LineID;
        wResult.LineName = wOMSOrder.LineName;
        wResult.LineCode = wOMSOrder.LineCode;
        wResult.CustomerID = wOMSOrder.CustomerID;
        wResult.CustomerName = wOMSOrder.CustomerName;
        wResult.CustomerCode = wOMSOrder.CustomerCode;
        wResult.OrderID = wOMSOrder.ID;
        wResult.OrderNo = wOMSOrder.Code;
        wResult.PartNo = wOMSOrder.PartNo;

        wResult.DemandDate = wMaterialItemList.get(0).DemandDate;
        wResult.MaterialID = wMaterialItemList.get(0).MaterialID;
        wResult.MaterialName = wMaterialItemList.get(0).MaterialName;
        wResult.MaterialCode = wMaterialItemList.get(0).MaterialCode;
        wResult.MaterialTypeID = wMaterialItemList.get(0).MaterialTypeID;
        wResult.MaterialTypeName = wMaterialItemList.get(0).MaterialTypeName;
        wResult.MaterialTypeCode = wMaterialItemList.get(0).MaterialTypeCode;
        wResult.MaterialGroupID = wMaterialItemList.get(0).MaterialGroupID;
        wResult.MaterialGroupCode = wMaterialItemList.get(0).MaterialGroupCode;
        wResult.MaterialGroupName = wMaterialItemList.get(0).MaterialGroupName;
        wResult.MaterialFullGroup = wMaterialItemList.get(0).MaterialFullGroup;
        wResult.UnitID = wMaterialItemList.get(0).UnitID;
        wResult.UnitText = wMaterialItemList.get(0).UnitText;
        wResult.UnitCode = wMaterialItemList.get(0).UnitCode;
        wResult.PlanFQTY = wMaterialItemList.stream().mapToDouble(p -> p.PlanFQTY).sum();

        wResult.ItemList = wMaterialItemList;

        return wResult;
    }


    /**
     * 查出所有存在配送需求，
     * 对比主键如果存在就修改,只能修改计划数，不能修改已配料数 ，
     * 如果不存在new
     * 如果存在已配的配送需求，且当日需求数量降低时，将后续日期的需求减少对应数量
     * 一般物料已配送，不会变更计划与物料配送需求
     * 配送不指定工位，谁收都行，统一由工序库存物料
     *
     * @param wOMSOrder
     * @param wAPSTaskPartPointDayList
     * @param wMSSBOMItem
     */
    public MRPDeliveryItem MRP_CreateDeliveryItem(OMSOrder wOMSOrder,
                                                  List<APSTaskPartPointDay> wAPSTaskPartPointDayList, MSSBOMItem wMSSBOMItem) {
        MRPDeliveryItem wResult = new MRPDeliveryItem();
        wResult.FactoryID = wOMSOrder.FactoryID;
        wResult.FactoryName = wOMSOrder.FactoryName;
        wResult.FactoryCode = wOMSOrder.FactoryCode;
        wResult.ProductID = wOMSOrder.ProductID;
        wResult.ProductCode = wOMSOrder.ProductCode;
        wResult.ProductName = wOMSOrder.ProductName;
        wResult.LineID = wOMSOrder.LineID;
        wResult.LineName = wOMSOrder.LineName;
        wResult.LineCode = wOMSOrder.LineCode;
        wResult.CustomerID = wOMSOrder.CustomerID;
        wResult.CustomerName = wOMSOrder.CustomerName;
        wResult.CustomerCode = wOMSOrder.CustomerCode;
        wResult.PartNo = wOMSOrder.PartNo;
        wResult.OrderID = wOMSOrder.ID;
        wResult.OrderNo = wOMSOrder.Code;
        wResult.WBSNo = wOMSOrder.WBSNo;

        wResult.MaterialID = wMSSBOMItem.MaterialID;
        wResult.MaterialName = wMSSBOMItem.MaterialName;
        wResult.MaterialCode = wMSSBOMItem.MaterialNo;
        wResult.MaterialTypeID = wMSSBOMItem.MaterialTypeID;
        wResult.MaterialTypeName = wMSSBOMItem.MaterialTypeName;
        wResult.MaterialTypeCode = wMSSBOMItem.MaterialTypeCode;
        wResult.MaterialGroupID = wMSSBOMItem.MaterialGroupID;
        wResult.MaterialGroupCode = wMSSBOMItem.MaterialGroupCode;
        wResult.MaterialGroupName = wMSSBOMItem.MaterialGroupName;
        wResult.MaterialFullGroup = wMSSBOMItem.MaterialFullGroup;
        wResult.UnitID = wMSSBOMItem.UnitID;
        wResult.UnitText = wMSSBOMItem.UnitText;
        wResult.UnitCode = wMSSBOMItem.UnitCode;

        wResult.StandardNum = wMSSBOMItem.MaterialNumber;
        if (wResult.StandardNum <= 0)
            wResult.StandardNum = 1;

        wResult.ReplaceType = wMSSBOMItem.ReplaceType;
        wResult.OutSourceType = wMSSBOMItem.OutsourceType;
        wResult.OriginalType = wMSSBOMItem.OriginalType;
        wResult.DisassyType = wMSSBOMItem.DisassyType;
        wResult.Status = MRPDeliveryStatus.Todo.getValue();


        wResult.RoutePartID = wAPSTaskPartPointDayList.get(0).RoutePartID;
        wResult.RoutePartName = wAPSTaskPartPointDayList.get(0).RoutePartName;
        wResult.RoutePartPointID = wAPSTaskPartPointDayList.get(0).RoutePartPointID;
        wResult.RoutePartPointName = wAPSTaskPartPointDayList.get(0).RoutePartPointName;
        wResult.DemandDate = wAPSTaskPartPointDayList.get(0).StationDate;
        wResult.StationID = wAPSTaskPartPointDayList.stream().flatMap(p -> p.StationID.stream()).collect(Collectors.toList());
        wResult.OrderPlan = wAPSTaskPartPointDayList.stream().mapToDouble(p -> p.PlantNum).sum();
        wResult.PlanFQTY = wResult.StandardNum * wResult.OrderPlan;


        return wResult;
    }


}
